#property copyright "Dimitri Rudel 16.10.2013 new"
#property link      ""


#include <basic.mqh>
#include <tradeManager.mqh>




extern double exRsi = 14;
extern double exBb = 90;
extern double exBbDeviation = 4;
extern double exAdx = 14;

extern int exMa =14;
extern int exPip = 10;

extern int exEntryMaxRsi = 70;
extern int exEntryMinRsi = 30;

extern int adxMax = 90;
extern int adxMin =10;

extern int exCntCandleBbmOut = 1;

extern int exSlTrailPeriod = 1;
extern int exMaParent = 200;

double bbLower = 0;
double bbMid = 0;
double bbUpper = 0;
int adx = 0;
int adx2 = 0;
double rsi = 0;
double ma = 0;
double maParent = 0;

datetime oldTime;

int init()
  {
//----
       b_debug("BBM INIT");
      oldTime = Time[0];
   
//----
   return(0);
  }

int deinit()
  {
        b_debug("BBM DEINIT");
   return(0);
  }


int start()
  {     
     if(Bars < exBb){
         return(0);
     }
     if(oldTime == Time[0] ){
         return(0);   
      }
     oldTime = Time[0];

    
      bbLower = iBands(Symbol(),0,exBb,exBbDeviation,0,PRICE_CLOSE,MODE_LOWER,1); 
      bbUpper = iBands(Symbol(),0,exBb,exBbDeviation,0,PRICE_CLOSE,MODE_UPPER,1);  
      bbMid = (bbLower + bbUpper)/2;
      ma = iMA(Symbol(),0,exMa,0,MODE_SMA,PRICE_CLOSE,1);
      maParent = iMA(Symbol(),0,exMaParent,0,MODE_SMA,PRICE_CLOSE,1);
      
      
      adx = iADX(Symbol(),0,exAdx,PRICE_CLOSE,MODE_MAIN,1);
      adx2 = iADX(Symbol(),0,exAdx,PRICE_CLOSE,MODE_MAIN,2);
      rsi = iRSI(Symbol(),0,exRsi,PRICE_CLOSE,1);
 
      //tmgr_checkExit();
       b_exitByMa();
      if(!b_isTradeTime()){
         return(0);
      }

      
      
      
      bool trade = false;
      int cmd = -1;
      double price = 0;
      double sl = 0;
      double tp = 0;
      int pColor = 0;
      if(Close[1] > bbMid && Close[1] > ma && Close[2] < bbMid && rsi < exEntryMaxRsi ){
         cmd = OP_BUY;
         trade = true;
         price = Ask;
         pColor = Green;      

      }else if(Close[1] < bbMid && Close[1] < ma && Close[2] > bbMid && rsi > exEntryMinRsi ){
         cmd = OP_SELL;
         trade = true;
         price = Bid; 
         pColor = Red; 
      }
      
      if(trade){
         if(cmd == OP_BUY){

         
            b_exitPositionsByType(OP_SELL);
            sl = bbLower;
            //sl = Low[1];
         }else{

            b_exitPositionsByType(OP_BUY);
            sl = bbUpper;
            //sl = High[1];
         }
         if(b_getOpenPositionsByType(cmd) == 0){
            b_OrderSend(Symbol(),cmd,0.1,price,3,sl,0,"bbm",0,0,pColor); 
         }
         
      }
      
      
      
      
   return(0);
  }
  


void b_exitByMa(){
   for(int i=0; i < OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         double bbLower2 = iBands(Symbol(),0,exBb,exBbDeviation,0,PRICE_CLOSE,MODE_LOWER,exSlTrailPeriod); 
         double bbUpper2 = iBands(Symbol(),0,exBb,exBbDeviation,0,PRICE_CLOSE,MODE_UPPER,exSlTrailPeriod);  
         double bbMid2 = (bbLower2 + bbUpper2)/2;

         

         if ( OrderSymbol()==Symbol()) {
            //if(iBarShift(Symbol(),0,OrderOpenTime())>=exSlTrailPeriod){
            //   b_OrderModify(OrderTicket(),OrderOpenPrice(),bbMid2,OrderTakeProfit());
            //}
            bool close = false;
            if(OrderType() == OP_BUY){
               //close = Low[1] < ma + b_getPriceByPip(exSlDeviationPip);
               //close = Close[1] < bbMid && Close[1] < ma;
               close = High[1] < bbLower;// || (rsi > exEntryMaxRsi && Close[1] > bbUpper);
            }else{
               //close = High[1] > ma - b_getPriceByPip(exSlDeviationPip);
               //close = Close[1] > bbMid && Close[1] > ma ;
               close = Low[1] > bbUpper;// || (rsi < exEntryMinRsi && Close[1] < bbLower);
            
            }
            if(close){
               OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,Red);  
            }              
         }
      }
   }
}


